/**
 *
 */
package com.example.office.util.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author jujh
 * @Date 2018-08-03
 */
public class HttpClientHelper {
    private static Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);

    public static String send(String httpSendUrl, Map<String, String> params) throws Exception {
        if (!httpSendUrl.substring(httpSendUrl.length() - 1).equals("?")) {
            httpSendUrl += "?";
        }
        Set<String> keys = params.keySet();
        for (String key : keys) {
//            httpSendUrl += key + "=" + URLEncoder.encode(params.get(key), "utf-8") + "&";
            httpSendUrl += key + "=" + params.get(key) + "&";
        }
        if (keys != null && keys.size() > 0) {
            httpSendUrl = httpSendUrl.substring(0, httpSendUrl.length() - 1);
        }
        logger.info(httpSendUrl);
        URL url = new URL(httpSendUrl);
        HttpURLConnection reqConn = (HttpURLConnection) url.openConnection();
        reqConn.setDoInput(true);
        reqConn.setDoOutput(true);
        reqConn.setReadTimeout(1000*10);
        reqConn.setConnectTimeout(1000*20);
        reqConn.setRequestMethod("GET");
        reqConn.addRequestProperty("Content-Type", "application/xml; charset=utf-8");
        reqConn.connect();

        String result = null;
        if (reqConn.getResponseCode() == 200) {
            InputStream isdata = reqConn.getInputStream();
            InputStreamReader isr = new InputStreamReader(isdata);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            result = sb.toString();

        }
        return result;
    }

    public static void main(String[] argss) throws Exception {

        Map<String, String> params = new HashMap<String, String>();
        params.put("reqSno", "");
        params.put("payType", "");
        params.put("payMark", "");
        params.put("payAmount", "");
        params.put("onTime", "");
        params.put("waitTime", "");
        params.put("mileage", "");
        params.put("vehiNo", "");
        params.put("jobCd", "");
        params.put("telCallId", "");
        System.out.println("--------->"
                + HttpClientHelper.send("http://www.baidu.com", params));
    }

}
